package com.yunchen.server.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yunchen.server.pojo.Permissions;
import com.yunchen.server.service.PermissionsService;
import com.yunchen.server.util.Return;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/Permissions")
@RestController
@Transactional
@CrossOrigin
public class PermissionsController {
    @Autowired
    private PermissionsService permissionsService;
    @PostMapping("/Add")
    public Return Add(@RequestBody Permissions permissions){
        Return re = new Return();
        try {
            re = permissionsService.Add(permissions);
        }catch  (Exception e) {
            e.printStackTrace();
            re.setCode(500);
            re.setMsg("操作失败");
        }
        return re;
    }
    @PostMapping("/Update")
    public Return Update(@RequestBody Permissions permissions){
        Return re = new Return();
        try {
            re = permissionsService.Update(permissions);
        }catch  (Exception e) {
            e.printStackTrace();
            re.setCode(500);
            re.setMsg("操作失败");
        }
        return re;
    }
    @PostMapping("/Del")
    public Return Del(@RequestBody Permissions permissions){
        Return re = new Return();
        try {
            re = permissionsService.Del(permissions);
        }catch  (Exception e) {
            e.printStackTrace();
            re.setCode(500);
            re.setMsg("操作失败");
        }
        return re;
    }
    @PostMapping("/List")
    public Return List(@RequestBody JSONObject json){
        Return re = new Return();
        try {
            JSONArray list = permissionsService.List(json);
            re.setData(list);
            re.setCode(200);
            re.setMsg("成功");
        }catch  (Exception e) {
            e.printStackTrace();
            re.setCode(400);
            re.setMsg("操作失败");
        }
        return re;
    }
}
